package com.relife.sys.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.Client;
import com.relife.common.model.ClientManager;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.common.workflow.HistoryProcessInstanceDiagramCmd;
import com.relife.core.easyui.Easyui;
import com.relife.core.entity.CodeEntity;
import com.relife.core.entity.ProcessSetEntity;
import com.relife.core.service.ProcessService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.interceptor.Command;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/**
 * 
 * @author WU
 * @date 2016年6月3日 上午11:20:46
 */
@Controller
@RequestMapping("sys/process")
public class ProcessController extends BaseController {

	@Autowired
	private ProcessService processService;

	/**
	 * 
	 * @author WU
	 * @date 2016年6月3日 上午11:21:19
	 * @param req
	 * @return
	 */
	
	@RequestMapping
	public String processList(HttpServletRequest req) {
		return "sys/process/processList";
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年6月3日 上午11:21:19
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String processImage(HttpServletRequest req) {
		return "sys/process/processImage";
	}

	/**
	 * 待办任务列表
	 * 
	 * @author WU
	 * @date 2016年6月3日 上午11:28:17
	 * @param name
	 * @param page
	 * @param res
	 */
	@RequestMapping
	public void taskListData(String businesskey, String processkey, String name, String processtype, Page page, HttpServletResponse res) {
		Client client = ClientManager.getInstance().getClientSession();
		List<Object> param = new ArrayList<Object>();
		param.add(client.getCompany().getDepartCode() + "%");
		param.add(client.getUser().getId());
		param.add(client.getUser().getId());
		param.add(client.getCompany().getId());
		StringBuffer where = new StringBuffer("");

		if (StringUtil.isNotEmpty(processkey)) {
			where.append("  and ps.processkey = ?");
			param.add(processkey);
		}
		if (StringUtil.isNotEmpty(processtype)) {
			where.append("  and ps.processtype = ?");
			param.add(processtype);
		}

		if (StringUtil.isNotEmpty(businesskey)) {
			where.append("  and ae.business_key_ like ?");
			param.add("%" + businesskey + "%");
		}
		if (StringUtil.isNotEmpty(name)) {
			where.append("  and u.name like ?");
			param.add("%" + name + "%");
		}
		processService.taskListData(page, where, param.toArray());
		page.setFields("businesskey,taskid,pid,nodename,nodekey,processname,url,applydate,applyuser,processkey,w,h");
		Easyui.datagrid(page, res);
	}

	/**
	 * 我的已办工作
	 * 
	 * @author WU
	 * @date 2016年6月3日 下午5:10:31
	 * @param page
	 * @param res
	 */
	@RequestMapping
	public void historyList(String businesskey, String processkey, String name, String processtype, Page page, HttpServletResponse res) {
		Client client = ClientManager.getInstance().getClientSession();
		StringBuffer where = new StringBuffer("");
		List<Object> param = new ArrayList<Object>();
		param.add(client.getUser().getId());
		param.add(client.getCompany().getDepartCode() + "%");
		if (StringUtil.isNotEmpty(processkey)) {
			where.append("  and sp.processkey = ?");
			param.add(processkey);
		}
		if (StringUtil.isNotEmpty(processtype)) {
			where.append("  and sp.processtype = ?");
			param.add(processtype);
		}
		if (StringUtil.isNotEmpty(businesskey)) {
			where.append("  and pa.businesskey like ?");
			param.add("%" + businesskey + "%");
		}
		if (StringUtil.isNotEmpty(name)) {
			where.append("  and us.name like ?");
			param.add("%" + name + "%");
		}

		processService.historyListData(page, where, param.toArray());
		page.setFields("businesskey,pid,pdid,suid,starttime,endtime,processname,name,url,w,h");
		Easyui.datagrid(page, res);
	}

	/**
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public JSONArray getProcess() {
		JSONArray array = new JSONArray();
		List<CodeEntity> list = ClientManager.getInstance().getCodeList("processtype");
		for (int i = 0; i < list.size(); i++) {
			JSONObject obj = new JSONObject();
			obj.put("id", list.get(i).getValue());
			obj.put("name", list.get(i).getName());
			obj.put("type", "processtype");
			obj.put("open", true);
			obj.put("pId", "pId");
			array.add(obj);
		}
		List<ProcessSetEntity> setList = service.getList(ProcessSetEntity.class, Order.desc("createDate"));
		for (int i = 0; i < setList.size(); i++) {
			JSONObject obj = new JSONObject();
			obj.put("id", setList.get(i).getProcesskey());
			obj.put("name", setList.get(i).getProcessname());
			obj.put("pId", setList.get(i).getProcesstype());
			obj.put("type", "processkey");
			obj.put("open", true);
			array.add(obj);
		}
		JSONObject obj = new JSONObject();
		obj.put("id", "pId");
		obj.put("name", "全部");
		obj.put("type", "all");
		obj.put("open", true);
		array.add(obj);
		return array;
	}

	/**
	 * 读取带跟踪的流程图片
	 * 
	 * @author WU
	 * @date 2016年6月6日 下午5:14:34
	 * @param pId
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping
	public void traceImage(String pId, HttpServletResponse res) throws Exception {
		Command<InputStream> cmd = new HistoryProcessInstanceDiagramCmd(pId);
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		InputStream is = processEngine.getManagementService().executeCommand(cmd);
		int len = 0;
		byte[] b = new byte[1024];
		while ((len = is.read(b, 0, 1024)) != -1) {
			res.getOutputStream().write(b, 0, len);
		}
	}

	/**
	 * 流转记录
	 */
	@RequestMapping
	public void flowListData(String pId, HttpServletResponse res) {
		Page page = new Page();
		page.setList(processService.flowList(pId));
		page.setFields("pId,nodeName,startTime,endTime,processType,userName,msg");
		Easyui.datagrid(page, res);
	}

}
